package org.tuo.controller;

import org.fhawk.tinyspring.ioc.annotation.inject;
import org.fhawk.tinyspring.mvc.annotation.Controller;
import org.fhawk.tinyspring.mvc.annotation.Requset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tuo.bean.Student;
import org.tuo.service.StudentService;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tuo on 9/29/16.
 */
@Controller
public class StudentController {
    private Logger logger = LoggerFactory.getLogger(StudentController.class);

    @inject
    private StudentService service;


    @Requset
    public Map<String, Object> getStudent(int id) {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", service.get(id));
        } catch (SQLException e) {
            result.put("code", 500);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    @Requset
    public Map<String, Object> addStudent(String name, String money) {
        logger.info("parameters:name:{},id:{},money:{}", name, money);
        Student student = new Student();
        student.setName(name);
        student.setMoney(BigDecimal.valueOf(Double.valueOf(money)));
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "SUCCESS");
        try {
            service.update(student);
        } catch (SQLException e) {
            result.put("code", 500);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    @Requset
    public Map<String, Object> students() {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", service.findAll());
        } catch (SQLException e) {
            result.put("code", 500);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    @Requset
    public List<Map<String, Object>> list(String name) {
        return service.findList(name);
    }
}
